#include<stdio.h>
#include<sys/dir.h>
#include<unistd.h>
#include<sys/types.h>
#include"gui_window.h"
#include"gui_list.h"
#include"fm_core.h"
#include<sys/walltime.h>

fm_dir_t root_dir;

//cwd update in file fm_list,fm_list_button_down function
int fm_rootdir_open()
{
    //open root dir
    DIR *dir=opendir(".");
    if(!dir)
    {
        dprintf("[fm] open dir error\n");
        return -1;
    }
    dprintf("[fm] open dir / ok\n");

    root_dir.dir_fd=dir;
    strcpy(root_dir.name,"/");
    root_dir.fstu=NULL;
    root_dir.type=FM_DIR_ROOT;
    root_dir.d_counts=fm_dir_get_counts(dir,0);

    //show dir item
    fm_dir_show(root_dir.dir_fd);

    //refresh screen
    fm_list_show();

    closedir(dir);
}

//open a cutsom director
int fm_dir_open(char *path)
{
    if(!path||!path[0])
    {    
        dprintf("[fm] invaild path format\n");
        return -1;
    }

    DIR *dir=opendir(path);
    if(!dir)
    {
        dprintf("[fileman] %s open dir err\n",path);
        return -1;
    }
    dprintf("[fileman] open dir ok\n");

    root_dir.dir_fd=dir;
    root_dir.type=FM_DIR_CUSTOM;
    root_dir.d_counts=fm_dir_get_counts(dir,0);
    root_dir.fstu=NULL;
    strcpy(root_dir.name,path);

    fm_dir_show(root_dir.dir_fd);

    //refresh screen
    fm_list_show();
}

void fm_dir_show(DIR *dir)
{
    dirent_t *dirent;
    uint32_t idx=0;

    while(dirent=readdir(dir))
    {
        //dprintf("[dir] idx %d dir show: name %s attr %x date %x\n",idx,dirent->d_name,dirent->d_attr,dirent->d_date);

        char buff[32];
        fm_list_show_name(idx++,dirent->d_name,fm_get_type_string(dirent->d_name,dirent->d_attr),fm_dir_get_date(dirent->d_date,dirent->d_time),itoa(dirent->d_size,10,buff,0));
    }

    dprintf("[dir] read dir end\n");
}

int fm_core_init()
{
    //open root dir
    fm_rootdir_open();
}

int fm_dir_printf(DIR *dir)
{
    dirent_t *dirent;
    while(dirent=readdir(dir))
    {
        dprintf("[dir] name %s attr %x date %x\n",dirent->d_name,dirent->d_attr,dirent->d_date);
    }
    rewinddir(dir);
}

char *fm_get_type_string(char *name,int attr)
{
    if(!name||!name[0])
    {
        return NULL;
    }

    int i=0;
    char *p=name;

    #ifdef DEBUG_FM_INFO
    dprintf("get type string: name %s attr %x ",name,attr);
    #endif

    if(attr==DIRENT_DIR)
       return "director";

    while(*p!='\0')
    {
        if(*p=='.')
            break;
        p++;
    }

    if(*p=='\0')
    {
        return "file";
    }

    char *type_str="NULL";
    name=p;

    if(!strcmp(name,".txt"))
    {
        type_str="text";
    }
    else 
    {
        if(!strcmp(name,".bin"))
            type_str="bin";
        else 
            if(!strcmp(name,".wav")||!strcmp(name,".mp3")||!strcmp(name,"avi"))
                type_str="music";
            else if(!strcmp(name,".mp4"))
                type_str="video";
            else 
                type_str="file";
    }
    #ifdef DEBUG_FM_INFO
    dprintf("type: %s\n",type_str);
    #endif
    return type_str;
}

char date_buff[32];

char *fm_dir_get_date(uint32_t date,uint32_t time)
{
    sprintf(date_buff,"%04d/%02d/%02d %02d:%02d:%02d\n",WALLTIME_RD_YEAR(date >> 16), WALLTIME_RD_MON(date >> 16), WALLTIME_RD_DAY(date >> 16), WALLTIME_RD_HOUR(time & 0xffff), WALLTIME_RD_MIN(time & 0xffff), WALLTIME_RD_SEC(time & 0xffff));
    #ifdef DEBUG_FM_INFO
    dprintf("get date: %s\n",date_buff);
    #endif
   return date_buff;
}

//get dir all sub dir and file counts
uint32_t fm_dir_get_counts(DIR *dir,int type)
{
    dirent_t *dirent;
    uint32_t counts=0;
    while(dirent=readdir(dir))
    {
        counts++;
    }
    rewinddir(dir);
    dprintf("count %d ",counts);
    return counts;
}

int fm_dir_rename(char *old_name,char *new_name)
{
    char buff1[MAX_PATH_LEN];
    char buff2[MAX_PATH_LEN];
    memset(buff1,0,MAX_PATH_LEN);
    memset(buff2,0,MAX_PATH_LEN);
    buildpath(old_name,buff1);
    buildpath(new_name,buff2);
    dprintf("rename %s to %s\n",buff1,buff2);
    if(rename(buff1,buff2)<0)
    {
        dprintf("[fm] rename %s to %s error\n",old_name,new_name);
        return -1;
    }
    dprintf("[fm] rename: rename ok\n");

    return 0;
}

int fm_dir_delete(char *name)
{
    char buff[MAX_PATH_LEN];
    memset(buff,0,MAX_PATH_LEN);
    buildpath(name,buff);
    if(remove(buff)<0)
    {
        dprintf("[fm] delete %s error\n",buff);
        return -1;
    }
    dprintf("[fm] delete ok\n");
    return 0;
}

char *fm_get_cwd()
{
    return root_dir.name;
}